﻿
    using Chain.BLL;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public partial class GoodsAdd : PageBase
    {


        protected void bindSyncShopList()
        {
            Chain.BLL.SysShop bllSS = new Chain.BLL.SysShop();
            string sqlStr = "ShopID>0 and ShopID<>" + base._UserShopID;
            sqlStr = PubFunction.GetShopAuthority(base._UserShopID, "ShopID", sqlStr);
            DataTable dt = bllSS.GetList(sqlStr).Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.rptSyncShopList.DataSource = dt;
                this.rptSyncShopList.DataBind();
            }
            else
            {
                this.lblShowSync.Visible = false;
                this.lblShowSyncPartial.Visible = false;
            }
        }

        protected void GetGoodsInfo(int goodsID)
        {
            Chain.BLL.Goods bllGoods = new Chain.BLL.Goods();
            Chain.Model.Goods modelGoods = bllGoods.GetModel(goodsID);
            DataTable dtGoods = bllGoods.GetItemAll(goodsID).Tables[0];
            this.txtGoodsID.Value = modelGoods.GoodsID.ToString();
            this.txtGoodsCode.Value = modelGoods.GoodsCode;
            this.txtCode.Value = modelGoods.GoodsCode;
            if (modelGoods.GoodsType == 1)
            {
                this.chkService.Checked = true;
            }
            this.txtGoodsName.Value = modelGoods.Name;
            this.txtGoodsNameCode.Value = modelGoods.NameCode;
            PubFunction.BindGoodsClass(this.sltGoodsClass, modelGoods.CreateShopID);
            this.sltGoodsClass.Value = modelGoods.GoodsClassID.ToString();
            this.sltjldw.Value = modelGoods.Unit;
            this.sltCommissionType.Value = modelGoods.CommissionType.ToString();
            this.txtCommissionNumber.Value = modelGoods.CommissionNumber.ToString();
            this.txtGoodsPrice.Value = Math.Round(modelGoods.Price, 2).ToString();
            if (modelGoods.Point != -1)
            {
                this.txtGoodsPoint.Value = modelGoods.Point.ToString();
            }
            else
            {
                this.txtGoodsPoint.Value = "";
            }
            this.txtGoodsBidPrice.Value = Math.Round(modelGoods.GoodsBidPrice, 2).ToString();
            this.txtGoodsMinPercent.Value = modelGoods.MinPercent.ToString();
            this.txtGoodsRemark.Value = modelGoods.GoodsRemark;
            this.txtGoodsSalePercent.Value = modelGoods.SalePercet.ToString();
            PubFunction.BindEditCustomFields(this.tbCustomField, "goods", dtGoods.Rows[0]);
            PubFunction.BindShopSelect(base._UserShopID, this.sltShopList, modelGoods.CreateShopID, base._UserShopID != 1);
            this.txtShopID.Value = modelGoods.CreateShopID.ToString();
            this.txtShopID.Value = modelGoods.CreateShopID.ToString();
        }

        protected void GetGoodsNumber(int goodsID, int shopID)
        {
            this.trGoodsNumber.Attributes.CssStyle.Add("display", "");
            new Chain.Model.GoodsNumber();
            Chain.BLL.GoodsNumber bllNumber = new Chain.BLL.GoodsNumber();
            DataTable dt = new DataTable();
            if (shopID != 0)
            {
                dt = bllNumber.GetList(string.Concat(new object[] { " GoodsID=", goodsID, " and ShopID=", shopID })).Tables[0];
            }
            else
            {
                dt = bllNumber.GetList(string.Concat(new object[] { " GoodsID=", goodsID, " and ShopID=", base._UserShopID })).Tables[0];
            }
            this.txtGoodsNumber.Value = dt.Rows[0]["Number"].ToString();
            this.hdShopID.Value = dt.Rows[0]["ShopID"].ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.txtCode.Value = DateTime.Now.ToString("yyMMddHHmmss");
                PubFunction.BindGoodsClass(this.sltGoodsClass, base._UserShopID);
                this.sltGoodsClass.Value = "1";
                if (!string.IsNullOrEmpty(base.curParameter.strUnitList))
                {
                    foreach (string strdw in base.curParameter.strUnitList.Split(new char[] { '|' }))
                    {
                        this.sltjldw.Items.Add(new ListItem(strdw, strdw));
                    }
                }
                else
                {
                    this.sltjldw.Items.Add(new ListItem("个", "个"));
                }
                if (base.Request.QueryString["GoodsID"] != null)
                {
                    this.GetGoodsInfo(int.Parse(base.Request.QueryString["GoodsID"]));
                    this.GetGoodsNumber(int.Parse(base.Request.QueryString["GoodsID"]), int.Parse(base.Request.QueryString["ShopID"]));
                    this.ltlTitle.Text = "主页   >   商品管理   >   编辑商品 ";
                }
                else
                {
                    PubFunction.BindAddCustomFields(this.tbCustomField, "goods");
                }
                if (base.Request.QueryString["GoodsID"] != null)
                {
                    Chain.Model.Goods goods = new Chain.BLL.Goods().GetModel(int.Parse(base.Request.QueryString["GoodsID"]));
                    PubFunction.BindShopSelect(base._UserShopID, this.sltShopList, goods.CreateShopID, true);
                    this.txtShopID.Value = goods.CreateShopID.ToString();
                }
                else
                {
                    PubFunction.BindShopSelect(base._UserShopID, this.sltShopList, base._UserShopID, true);
                    this.txtShopID.Value = this._UserShopID.ToString();
                    this.hdShopID.Value = this._UserShopID.ToString();
                }
                this.bindSyncShopList();
            }
        }
    }


